
	var member ;
	var order = {};
	var addressList ;
	var selectAddress ;	
	var orderItem = [];
	
	var buyNum;
	var freight=0;  //运费
	
	
	var source;
	apiready = function() {
        
        source=api.pageParam.source;  //来源

	    member = $api.getStorage('member');  //会员
	    	
		product = api.pageParam.product;
	    buyNum=product.num;
	    
	    calculation();  //计算费用
	    
		//显示购买的商品
		showBuyData();
		
		api.addEventListener({
		    name: 'addAddressSuccess'
		}, function(ret){
		    if(ret && ret.value){
		        addAddressSuccess(ret.value);
		    }
		});
		
		
		
		
		
	}
	

	
	//购买的商品
	var showBuyData = function() {
		api.showProgress({
	        title: '加载中...',
	        modal: false
	    });
		
		//获取地址    
		getAddress(function(alist){
      		addressList = alist;
      		showAddress();
      	});
      	
		showProduct();
		api.hideProgress();
	}
	
	
	var showAddress = function(){
    if(addressList)$api.byId('addressList').innerHTML = '';
    
	    	for(var i=0;i<addressList.length;i++){
	    		var aHtml = '';
	    		aHtml+='<li id="li'+i+'" class="" name="addressLi" onclick="doselectedAddress('+i+');">';
						aHtml+='<input type="radio" name="location"  value="'+i+'">';
						aHtml+='<p class="ng-binding">';
							aHtml+=addressList[i].receiver;
						aHtml+='</p>';
						aHtml+='<p class="am-text-truncate ng-binding">';
							aHtml+=addressList[i].province+addressList[i].city+addressList[i].district+'	&nbsp;'+addressList[i].address;
						aHtml+='</p>';
						aHtml+='<p class="ng-binding">';
							aHtml+=addressList[i].mobile;
						aHtml+='</p>';
						aHtml+='<span><i class="am-icon-check"></i></span>';
					aHtml+='</li>';
				
				$api.append($api.byId('addressList'), aHtml);
	    	}
	}
	
	
	
	var showProduct = function(){
			var aHtml = '';
			aHtml+='<div id="good_'+ product._id +'">';
				aHtml+='<div class="am-g am-padding-xs">';
					aHtml+='<div class="am-u-sm-3 am-text-center">';
						aHtml+='<img src="'+ serviceUrl +product.proDefImg +'">';
					aHtml+='</div>';
					aHtml+='<div class="am-u-sm-9">';
						aHtml+='<p class="am-text-xs ng-binding">';
							aHtml+=product.name;
						aHtml+='</p>';
						aHtml+='<p class="am-text-primary ng-binding" style="color:red;">';
							aHtml+='￥'+ product.price ;
						aHtml+='</p>';
					aHtml+='</div>';
					aHtml+='<div class="am-u-offset-sm-7 am-u-sm-5">';
						aHtml+='<div class="am-input-group am-input-group-sm">';
							aHtml+='<span class="am-input-group-btn">';
								aHtml+='<button class="am-btn am-btn-default" type="button" onclick="reduce();">';
									aHtml+='-';
								aHtml+='</button> </span>';
							aHtml+='<input type="number" class="am-form-field am-text-center" style="height:28px" id="buyNum" value="'+ (product.num||1) +'" readonly="">';
							aHtml+='<span class="am-input-group-btn">';
								aHtml+='<button class="am-btn am-btn-default" type="button" onclick="add();">';
									aHtml+='+';
								aHtml+='</button> </span>';
						aHtml+='</div>';
					aHtml+='</div>';
				aHtml+='</div>';
			aHtml+='</div>';
			
	
		  $api.html($api.byId('goodList'), aHtml);
 
			
	}
	
	var addOrder = function() {
	        
		   if(order.receiver==undefined){
		    	     toast('请选择收货信息');
				 return;
		    }
		
			orderItem={
			    'id' : product._id,
		    	'num' : buyNum,
		    	'price' : product.price,
		    	'total' :(product.price*buyNum).toFixed(2),
		    	'proDefImg' : product.proDefImg,
		    	'proName' : product.name,
		    	'merName' :product.merchantName,
		    	'purprice' : (product.purprice).toFixed(2),
		    	'code' : product.code,
			};
				
		    if(source=="抢购"){
		       order.buytype = "抢购";
		    }
				
			order.item = orderItem;
			order.payType=$api.val($api.dom('input[name="payType"]:checked'));
			order.memberAccount = member.account;
			order.remarks=$api.val($api.byId('remarks'));
			order.recman=$api.val($api.byId('recman'));
			order.source=api.deviceName;
			order.buyNum=buyNum;
		//	alert(JSON.stringify(order));
		
		
		
//		var orderinfo= {
//							orderNo: '11223444',
//							total: '120.00',
//							payType: '支付宝',
//						};
//					   openWin('order_ok',{orderinfo:orderinfo});
//					   
//					   
//					   return;
			
            do_ajax('/app/member/addorder2','post',order,function(respose,err){
				if (respose.resultCode == "0") {
		                api.hideProgress();
						var orderinfo= {
							orderNo: respose.orderNo,
							total: order.total,
							payType: order.payType,
						};
					   openWin('order_ok',{orderinfo:orderinfo});

				} else
					alert(respose.datas);
			});
			
			
			
		
	}
	

	/*
	 * 加法
	 */
	function add(){
	    	if(buyNum != -1) {
			++buyNum;
			$api.val($api.byId('buyNum'),buyNum);
		}
		calculation();
	};

	/*
	 * 减法
	 */
	function reduce(){
	    	if(buyNum>1) {
			--buyNum;
			$api.val($api.byId('buyNum'),buyNum);
		}
		calculation();
	};


	
	/**
	 * 计算费用
	 */
	var calculation = function() {
		
		var  proTotal=product.price*buyNum; //商品金额
		if(product.price==0){
	       freight=0;
	    }
		if(product.type!='服务产品'){
	          if(proTotal<88){
	             freight=12;
	          }
	          
	    }else{
	        freight=0;
	    }
	    
	    if(proTotal>88){
	        freight=0;
	    }
	    
	    if(proTotal>500){
	       if(product.type!='服务产品'){
	             freight=-20;
	       }
	    }
	    
	    order.proTotal=proTotal.toFixed(2);
	    order.freight=freight;
	    order.total=(proTotal+freight).toFixed(2);
	    order.rTotal=(proTotal+freight).toFixed(2);
	    
	    
	    
	    $api.text($api.byId('proTotal'), order.proTotal);
	    $api.text($api.byId('freight'),  order.freight);
	    $api.text($api.byId('total'), order.total);
	    

	}
	
	
	//添加收货地址
	var addAddressSuccess = function(addressInfo){
		$api.removeCls($api.dom('.c-address-list li'),'selected');
		addressList.push(addressInfo);
		var i = addressList.length-1;
		var aHtml = '';
    		aHtml+='<li id="li'+i+'" class="selected" onclick="doselectedAddress('+i+');">';
					aHtml+='<input type="radio" name="location"  value="'+i+'">';
					aHtml+='<p class="ng-binding">';
						aHtml+=addressInfo.receiver;
					aHtml+='</p>';
					aHtml+='<p class="am-text-truncate ng-binding">';
						aHtml+=addressInfo.province+addressInfo.city+addressInfo.district+'	&nbsp;'+addressInfo.address;
					aHtml+='</p>';
					aHtml+='<p class="ng-binding">';
						aHtml+=addressInfo.mobile;
					aHtml+='</p>';
					aHtml+='<span><i class="am-icon-check"></i></span>';
				aHtml+='</li>';
			
			$api.append($api.byId('addressList'), aHtml);
			
		
	}
	
	//选择收货地址
	var doselectedAddress = function(liindex){
		selectAddress = addressList[liindex];
		
	//	alert(JSON.stringify(selectAddress));
		
		order.mobile=selectAddress.mobile;
		
		
		
		order.receiver=selectAddress.receiver;
		order.province=selectAddress.province;
		order.city=selectAddress.city;
		order.district=selectAddress.district;

		order.currAddressDetail=selectAddress.province+selectAddress.city+selectAddress.district+selectAddress.address;
		
		$api.removeAttr($api.dom('.selected'),'class');
		$api.removeCls($api.dom('.selected'),'selected');
		$api.addCls($api.byId("li"+liindex), 'selected');
	}
	
	